package pannelli;
import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import database.DB;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;

import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import javax.swing.JFrame;


import oggetti.Riparazione;
import pannelli.main.PannelloRicerca;
import pannelli.main.PannelloRiparazioni;
import pannelli.sub.SubPannelloCliente;
import pannelli.sub.SubPannelloRiparazione;
import pannelli.sub.SubPannelloVisualizzaRiparazione;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class VisualizzaRiparazione extends javax.swing.JFrame {
	private SubPannelloRiparazione subPannelloRiparazione1;
	private JButton modifica;
	private Riparazione attuale;
	private JButton documento;

	public VisualizzaRiparazione(Riparazione r) {
		super();
		this.attuale = r;
		initGUI();
		setVisible(true);
		addWindowsListener();
	}
	private void addWindowsListener(){
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent arg0) {
                            
                            if (PannelloRiparazioni.getInstance()!=null)
                                    PannelloRiparazioni.getInstance().setFlag(false);
                            else
                                PannelloRicerca.getInstance().setFlag(false);
                                    
			}
		});
	}
	private void initGUI() {
		try {
			AnchorLayout thisLayout = new AnchorLayout();
			this.setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(703, 478));
			{
				documento = new JButton();
				getContentPane().add(documento, new AnchorConstraint(851, 950, 915, 581, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				documento.setText("Aggiungi a documento di trasporto");
				documento.setPreferredSize(new java.awt.Dimension(260, 29));
				documento.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						DocumentoDiTrasporto.getInstance().addRiparazione(attuale);
						//dispose();
					}
				});
			}
			{
				modifica = new JButton();
				getContentPane().add(modifica, new AnchorConstraint(851, 300, 915, 101, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				modifica.setText("Salva Riparazione");
				modifica.setPreferredSize(new java.awt.Dimension(140, 29));
				modifica.addActionListener(new ActionListener() {					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						Riparazione r = subPannelloRiparazione1.getRiparazioneFromFields();
						if(new DB().salvaModificheRiparazione(r)){
							JOptionPane.showMessageDialog(null,"Modifiche salvate con successo!");
                                                        if(r.getStato().equals("Riparato")){
                                                            new DB().addRiparazioneToUtente(r.getScheda(), r.getUtente());
                                                            int mese,anno;
                                                            String data[] = r.getDataConsegna().split("-");
                                                            mese = Integer.parseInt(data[1]);
                                                            anno = Integer.parseInt(data[2]);
                                                            new DB().addGuadagnoToMese(mese, anno, r.getPreventivo());
                                                        }
							dispose();
							SubPannelloVisualizzaRiparazione.getInstance().initTable();
							SubPannelloVisualizzaRiparazione.getInstance().setFlag(false);
						}else{
							JOptionPane.showMessageDialog(null,"Errore nel salvataggio delle modifiche!");
						}
					}
				});
			}
			{
				subPannelloRiparazione1 = new SubPannelloRiparazione();
				this.add(subPannelloRiparazione1, new AnchorConstraint(421, 1000, 810, 0, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				subPannelloRiparazione1.setPreferredSize(new java.awt.Dimension(703, 186));
				subPannelloRiparazione1.fillFields(attuale);
			}
			{
				SubPannelloCliente.getInstance().deleteAllFields();
				this.add(SubPannelloCliente.getInstance(), new AnchorConstraint(1, 1000, 408, 0, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				SubPannelloCliente.getInstance().setPreferredSize(new java.awt.Dimension(703, 195));
				SubPannelloCliente.getInstance().fillFields(new DB().getClienteFromID(""+attuale.getIdCliente()));
				SubPannelloCliente.getInstance().setEnabled(false);
				SubPannelloCliente.getInstance().disableAllField();
			}
			pack();
			centra();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void centra(){
		Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation(new Point((dimension.width - getSize().width) / 2, 
				(dimension.height - getSize().height) / 2 ));

	}

}
